package google.news;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import proxy.HttpProxy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zhongwei on 2017/1/28.
 * 找搜索结果多少条根据<div id="resultStats">
 約 0 件
 <nobr>这个来
 */
public class TestTest {

    // TODO 根据<div id="resultStats">(.+?)<nobr>来筛选结果

    public static void main(String[] args) throws IOException {
        long start1 = System.currentTimeMillis();
        String s1 = download("http://gg1.zzyjxs.com/search?q=DIEBOLD&safe=strict&tbs=cdr%3A1%2Ccd_min%3A10%2F11%2F2009%2Ccd_max%3A10%2F17%2F2009&tbm=nws");
        long end1 = System.currentTimeMillis();
        System.out.println(end1 - start1);
        long start = System.currentTimeMillis();
        downloadJsoup("http://gg1.zzyjxs.com/search?q=DIEBOLD&safe=strict&tbs=qdr:w&tbm=nws&source=lnt&sa=X&ved=0ahUKEwj9lLXazOnRAhUEwFQKHUZ-Bl8QpwUIFA&biw=1275&bih=589&dpr=2");
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    static HttpProxy getProxy() throws IOException {
        HttpClient client = HttpClients.createDefault();
        HttpResponse response = client.execute(new HttpGet("http://202.114.70.53:5454"));
        String content = EntityUtils.toString(response.getEntity());
        EntityUtils.consumeQuietly(response.getEntity());
        Pattern pattern = Pattern.compile("\"http://(.+?):(.+?)\"");
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            String host = matcher.group(1).trim();
            int port = Integer.parseInt(matcher.group(2).trim());
            return new HttpProxy(host, port);
        }
        return null;
    }

    public static String download(String url) throws IOException {
        HttpProxy httpProxy = getProxy();
        HttpClientBuilder builder = HttpClients.custom();
        RequestConfig.Builder requestBuilder = RequestConfig.custom();
        requestBuilder.setProxy(new HttpHost(httpProxy.getHost(), httpProxy.getPort()));
        builder.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36");
        List<Header> headers = new ArrayList<>();
        headers.add(new BasicHeader("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4"));
        builder.setDefaultHeaders(headers);
        HttpClient client = builder.build();
        HttpResponse response = client.execute(new HttpGet(url));
        String content = EntityUtils.toString(response.getEntity());
        EntityUtils.consumeQuietly(response.getEntity());
        return content;
    }

    public static String downloadJsoup(String url) throws IOException {
        HttpProxy httpProxy = getProxy();
        return Jsoup.connect(url).userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36").get().html();
    }
}
